<?php
$db = db::getInstance();
$forme = $_POST["select_form"];

// get the id form 
$get_id = new DonneesModel();
$filter = array(new DFC(DonneesModel::FIELD_FORM_NAME, $forme, DFC::EXACT));
$selected = $get_id->findByFilter($db, $filter);

foreach ($selected as $x) {
    $id = $x->getIdForm();
}

//extraire les users id ainsi que les dates 

$get_users = new UserDataModel();
$filter2 = array(new DFC(UserDataModel::FIELD_FORM_ID, $id, DFC::EXACT));
$user_id = $get_users->findByFilter($db, $filter2);

// extraire user login 
$get_login = new UsersListModel();

include("views/lang/lang_fr.php");
session_start();
if ($_SESSION['lang'] == "en") {
    include("views/lang/lang_en.php");
}
?>
<html>
    <head>


        <!-- include style css -->
        <link rel="stylesheet" href="../views/css/validationEngine.jquery.css" type="text/css"/>
        <link rel="stylesheet" href="../views/css/style_menu.css" type="text/css"/>
        <link rel="stylesheet" href="../views/css/mini3537.css" type="text/css"/>
        <link rel="stylesheet" href="../views/css/template.css" type="text/css"/>
        <link rel="stylesheet" href="../views/css/mini74d5.css" type="text/css"/>
        <link rel="stylesheet" href="../views/css/simplemodal.css" type="text/css" media="screen" title="no title" charset="utf-8">
        <link rel="icon" type="image/png" href="../views/image/dynamic_form.png" />

        <!-- include script js -->
        <script type="text/javascript" src="../views/js/jquery-1.6.min.js"></script>
        <script type="text/javascript" src="../views/js/ddaccordion.js"></script>
        <script type="text/javascript" src="../views/js/menu.js"></script>
        <script src="../views/js/simple-modal.js" type="text/javascript" charset="utf-8"></script>

        <script>
            $(document).ready(function(){
                
                // routage de l'action selon le bouton choisit'
                $(".supp").live("click",function(){
                    var id_supp = ($(this).parents("tr:first").children("td:first").text());
                    var id_form = ($(this).parents("tr:first").children("td:eq(1)").text());
              
                    $.post("delete_saved_form",{id :id_supp ,form:id_form});

                    $(this).parents("tr:first").remove();
            
                });
        
                $(".aff").live("click",function(){
                    var id_supp = ($(this).parents("tr:first").children("td:first").text());
                    var id_form = ($(this).parents("tr:first").children("td:eq(1)").text());
              
                    $.post("afficher_users_data",{id :id_supp ,form:id_form},
                    function(data) {  
                       
                        $(".affich").attr("style","visibility:visible;");
                        $("#div-truc").append(data);                        
                      
                    });
                    
                    $("#close").live("click",function(){
                        $(".affich").attr("style","visibility:hidden;");
                        $("#div-truc").html("");
                    });
            
                });
                
                $("#bouton_telecharger").live("click",function(){
                    var id_supp = ($(this).parents("tr:first").children("td:first").text());
                    var id_form = ($(this).parents("tr:first").children("td:eq(1)").text());
                    var name_form=$("#nf").text();
                    var name_user=($(this).parents("tr:first").children("td:eq(2)").text());
                    $.post("Telecharger",{id :id_supp ,form:id_form,nomform:name_form,nom_user:name_user});
                });
        
            });
        </script>

    </head>

    <body>
        <section id="message">
            <div class="block-border">
                <div class="block-content no-title dark-bg">
                    <p class="mini-infos"> <?php echo $titrconsult ?> </p>
                </div>
            </div>
        </section>

        <div class="block-border">
            <div class="block-content">

                <h1 id="nf"><?php echo $forme; ?></h1>


                <table class="table" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th scope="col">
                                <?php echo $user ?>   
                            </th>  
                            <th scope="col">
                                <?php echo $formulaire ?> 
                            </th>
                            <th scope="col">
                                <?php echo $userlog ?> 

                            </th>
                            <th scope="col">
                                <?php echo $dat ?> 
                            </th>
                            <th scope="col">
                                <?php echo $action ?>
                            </th>


                    </thead>
                    <!-- construction du tableau d'affichage des donnees enregistre par users -->
                    <?php
                    foreach ($user_id as $x) {
                        echo '<td>' . $x->getUserId() . '</td>
    <td>' . $x->getFormId() . '</td><td>';
                        $filter2 = array(new DFC(UsersListModel::FIELD_ID_USER, $x->getUserId(), DFC::EXACT));
                        $get_login = UsersListModel::findByFilter($db, $filter2);

                        foreach ($get_login as $y) {
                            echo $y->getLogin();
                        }
                        echo ' <img src="../views/image/user.png"></td>
 <td>' . $x->getDate() . '</td><td><button type="button" id="supprimer" class="grey supp">Supprimer</button>
     <button id="bouton_affiche" type="button" class="grey aff">Afficher</button><button id="bouton_telecharger" type="button" class="grey">Enregistrer</button> </td></tr>';
                    }
                    ?>

                </table>

            </div>

        </div>

        <div class="affich" style="visibility:hidden;">
            <div id="simple-modal" class="simple-modal draggable block-border" style="width: 400px; top: 40px; left: 431px;">
                <div class="simple-modal-header" style="cursor: move;">
                    <button type="button" id="close" class="red">x</button>
                </div>
                <div class="simple-modal-body">

                    <div id="div-truc">
                    </div>

                </div>
            </div>
        </div>

    </body>

</html>